﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>StrReplace - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The StrReplace function replaces the specified substring with a new string." />
<meta name="ahk:equiv-v1" content="commands/StringReplace.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>StrReplace</h1>

<p>Replaces the specified substring with a new string.</p>

<pre class="Syntax">NewStr := <span class="func">StrReplace</span>(Haystack, SearchText <span class="optional">, ReplaceText, OutputVarCount, Limit := -1</span>)</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>Haystack</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The string whose content is searched and replaced.</p>
  </dd>

  <dt>SearchText</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The string to search for. Matching is not case sensitive unless <a href="StringCaseSense.htm">StringCaseSense</a> has been turned on. </p>
  </dd>

  <dt>ReplaceText</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p><em>SearchText</em> will be replaced with this text. If omitted or blank, <em>SearchText</em> will be replaced with blank (empty). In other words, it will be omitted from the return value.</p>
  </dd>

  <dt>OutputVarCount</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#variables">变量</a></p>
    <p>The unquoted name of a variable in which to store the number of replacements that occurred (0 if none).</p>
  </dd>

  <dt>Limit</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>If <em>Limit</em> is omitted, it defaults to -1, which replaces <strong>all</strong> occurrences of the pattern found in <em>Haystack</em>. Otherwise, specify the maximum number of replacements to allow. The part of <em>Haystack</em> to the right of the last replacement is left unchanged.</p>
  </dd>

</dl>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
<p>This function returns the replaced version of the specified string.</p>

<h2 id="Remarks">备注</h2>
<p>The built-in variables <a href="../Variables.htm">A_Space</a> and <a href="../Variables.htm">A_Tab</a> contain a single space and a single tab character, respectively. They are useful when searching for spaces and tabs alone or at the beginning or end of <em>SearchText</em>.</p>

<h2 id="Related">相关</h2>
<p><a href="RegExReplace.htm">RegExReplace</a>, <a href="StringCaseSense.htm">StringCaseSense</a>, <a href="InStr.htm">InStr</a>, <a href="SubStr.htm">SubStr</a>, <a href="StrLen.htm">StrLen</a>, <a href="StrLower.htm">StrLower</a>, <a href="StrLower.htm">StrUpper</a></p>
<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre><em>; Remove all CR+LF's from the clipboard contents:</em>
Clipboard := StrReplace(Clipboard, "`r`n")

<em>; Replace all spaces with pluses:</em>
NewStr := StrReplace(OldStr, A_Space, "+")</pre>
</div>

<div class="ex" id="ExBlank">
<p><a href="#ExBlank">#2</a>: Remove all blank lines from the text in a variable:</p>
<pre>Loop
{
    MyString := StrReplace(MyString, "`r`n`r`n", "`r`n", ErrorLevel)
    if ErrorLevel = 0  <em>; No more replacements needed.</em>
        break
}</pre>
</div>

</body>
</html>